<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="总成编号" prop="zcCode">
        <el-input
          v-model="queryParams.zcCode"
          placeholder="请输入总成编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总成名称" prop="assembly_name">
        <el-input
          v-model="queryParams.assembly_name"
          placeholder="请输入总成名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预排产编号" prop="preProductionNumber">
        <el-input
          v-model="queryParams.preProductionNumber"
          placeholder="请输入预排产编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sales:assemblyPreSchedulingPlan:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sales:assemblyPreSchedulingPlan:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sales:assemblyPreSchedulingPlan:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sales:assemblyPreSchedulingPlan:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-unfold"
          size="mini"
          :disabled="multiple"
          @click="formalProduction"
        >正式排产
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assemblyPreSchedulingPlanList" @selection-change="handleSelectionChange" max-height="500px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" width="140"/>
      <el-table-column label="公司名称" align="center" prop="factoryName" width="140"/>
      <el-table-column label="合同号" align="center" prop="contract" width="140"/>
      <el-table-column label="订单号" align="center" prop="orderNo" width="140"/>
      <el-table-column label="订单类型" align="center" prop="orderType" width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.orderType=='1'">正式</span>
          <span v-if="scope.row.orderType=='2'">预测</span>
        </template>
      </el-table-column>
      <el-table-column label="交货日期" align="center" prop="jhDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jhDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="proType"/>
      <el-table-column label="产品编号" align="center" prop="productNumber" width="140"/>
      <el-table-column label="产品名称" align="center" prop="productName"  width="140"/>
      <el-table-column label="总成编号" align="center" prop="zcCode" width="140"/>
      <el-table-column label="总成名称" align="center" prop="assemblyName" width="140"/>
      <el-table-column label="部件编号" align="center" prop="partNumber" width="140"/>
      <el-table-column label="部件名称" align="center" prop="partName" width="140"/>
      <el-table-column label="上级排产编号" align="center" prop="parentProductionNumber" width="140"/>
      <el-table-column label="预排产编号" align="center" prop="preProductionNumber" width="140"/>
      <el-table-column label="预排产日期" align="center" prop="preProductionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.preProductionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划生产日期" align="center" prop="plannedProductionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plannedProductionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单数量" align="center" prop="orderNum"/>
      <el-table-column label="预排产数量" align="center" prop="preProductionQuantity" width="140"/>
      <el-table-column label="未排产数量" align="center" prop="unplannedQuantity" width="140"/>
      <el-table-column label="车间编码" align="center" prop="cjCode"/>
      <el-table-column label="车间名称" align="center" prop="workshopName"/>
      <el-table-column label="生产线代码" align="center" prop="lineId" width="140"/>
      <el-table-column label="生产线名称" align="center" prop="lineName" width="140"/>
      <el-table-column label="工位代码" align="center" prop="gwCode"/>
      <el-table-column label="工位名称" align="center" prop="gwName"/>
      <el-table-column label="工位物料代码" align="center" prop="gwItemCode" width="140"/>
      <el-table-column label="工位物料名称" align="center" prop="gwItemName" width="140"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="单台用量" align="center" prop="dtyl"/>
      <el-table-column label="订单用量" align="center" prop="ddyl"/>
      <el-table-column label="计划上线时间" align="center" prop="planOnlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planOnlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划下线时间" align="center" prop="planOfflineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planOfflineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对应整车图号" align="center" prop="dyZcNum" width="140"/>
      <el-table-column label="配置" align="center" prop="configuration"/>
      <el-table-column label="排放标准" align="center" prop="emiStand"/>
      <el-table-column label="排量" align="center" prop="displacement"/>
      <el-table-column label="额定功率" align="center" prop="ratedPower"/>
      <el-table-column label="机型" align="center" prop="machineType"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sales:assemblyPreSchedulingPlan:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sales:assemblyPreSchedulingPlan:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部装预排产计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司代码" prop="factoryCode">
          <el-input v-model="form.factoryCode" placeholder="请输入公司代码"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入公司名称"/>
        </el-form-item>
        <el-form-item label="合同号" prop="contract">
          <el-input v-model="form.contract" placeholder="请输入合同号"/>
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="交货日期" prop="jhDate">
          <el-date-picker clearable
                          v-model="form.jhDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择交货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总成编号" prop="zcCode">
          <el-input v-model="form.zcCode" placeholder="请输入总成编号"/>
        </el-form-item>
        <el-form-item label="总成名称" prop="assemblyName">
          <el-input v-model="form.assemblyName" placeholder="请输入总成名称"/>
        </el-form-item>
        <el-form-item label="部件编号" prop="partNumber">
          <el-input v-model="form.partNumber" placeholder="请输入部件编号"/>
        </el-form-item>
        <el-form-item label="部件名称" prop="partName">
          <el-input v-model="form.partName" placeholder="请输入部件名称"/>
        </el-form-item>
        <el-form-item label="上级排产编号" prop="parentProductionNumber">
          <el-input v-model="form.parentProductionNumber" placeholder="请输入上级排产编号"/>
        </el-form-item>
        <el-form-item label="预排产编号" prop="preProductionNumber">
          <el-input v-model="form.preProductionNumber" placeholder="请输入预排产编号"/>
        </el-form-item>
        <el-form-item label="预排产日期" prop="preProductionDate">
          <el-date-picker clearable
                          v-model="form.preProductionDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预排产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划生产日期" prop="plannedProductionDate">
          <el-date-picker clearable
                          v-model="form.plannedProductionDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择计划生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单数量" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单数量"/>
        </el-form-item>
        <el-form-item label="预排产数量" prop="preProductionQuantity">
          <el-input v-model="form.preProductionQuantity" placeholder="请输入预排产数量"/>
        </el-form-item>
        <el-form-item label="未排产数量" prop="unplannedQuantity">
          <el-input v-model="form.unplannedQuantity" placeholder="请输入未排产数量"/>
        </el-form-item>
        <el-form-item label="车间编码" prop="cjCode">
          <el-input v-model="form.cjCode" placeholder="请输入车间编码"/>
        </el-form-item>
        <el-form-item label="车间名称" prop="workshopName">
          <el-input v-model="form.workshopName" placeholder="请输入车间名称"/>
        </el-form-item>
        <el-form-item label="生产线代码" prop="lineId">
          <el-input v-model="form.lineId" placeholder="请输入生产线代码"/>
        </el-form-item>
        <el-form-item label="生产线名称" prop="lineName">
          <el-input v-model="form.lineName" placeholder="请输入生产线名称"/>
        </el-form-item>
        <el-form-item label="计划上线时间" prop="planOnlineTime">
          <el-date-picker clearable
                          v-model="form.planOnlineTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择计划上线时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划下线时间" prop="planOfflineTime">
          <el-date-picker clearable
                          v-model="form.planOfflineTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择计划下线时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="对应整车图号" prop="dyZcNum">
          <el-input v-model="form.dyZcNum" placeholder="请输入对应整车图号"/>
        </el-form-item>
        <el-form-item label="配置" prop="configuration">
          <el-input v-model="form.configuration" placeholder="请输入配置"/>
        </el-form-item>
        <el-form-item label="排放标准" prop="emiStand">
          <el-input v-model="form.emiStand" placeholder="请输入排放标准"/>
        </el-form-item>
        <el-form-item label="排量" prop="displacement">
          <el-input v-model="form.displacement" placeholder="请输入排量"/>
        </el-form-item>
        <el-form-item label="额定功率" prop="ratedPower">
          <el-input v-model="form.ratedPower" placeholder="请输入额定功率"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAssemblyPreSchedulingPlan,
  getAssemblyPreSchedulingPlan,
  delAssemblyPreSchedulingPlan,
  addAssemblyPreSchedulingPlan,
  updateAssemblyPreSchedulingPlan,
  formalProduction
} from "@/api/sales/assemblyPreSchedulingPlan";
import {getToken} from "@/utils/auth";

export default {
  name: "AssemblyPreSchedulingPlan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部装预排产计划表格数据
      assemblyPreSchedulingPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        factoryName: null,
        contract: null,
        orderNo: null,
        orderType: null,
        jhDate: null,
        proType: null,
        zcCode: null,
        assemblyName: null,
        partNumber: null,
        partName: null,
        parentProductionNumber: null,
        preProductionNumber: null,
        preProductionDate: null,
        plannedProductionDate: null,
        orderNum: null,
        preProductionQuantity: null,
        unplannedQuantity: null,
        cjCode: null,
        workshopName: null,
        lineId: null,
        lineName: null,
        planOnlineTime: null,
        planOfflineTime: null,
        dyZcNum: null,
        configuration: null,
        emiStand: null,
        displacement: null,
        ratedPower: null,
        machineType: null,
      },
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入`assemblyPreSchedulingPlan`信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sales/assemblyPreSchedulingPlan/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部装预排产计划列表 */
    getList() {
      this.loading = true;
      listAssemblyPreSchedulingPlan(this.queryParams).then(response => {
        this.assemblyPreSchedulingPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        factoryName: null,
        contract: null,
        orderNo: null,
        orderType: null,
        jhDate: null,
        proType: null,
        zcCode: null,
        assemblyName: null,
        partNumber: null,
        partName: null,
        parentProductionNumber: null,
        preProductionNumber: null,
        preProductionDate: null,
        plannedProductionDate: null,
        orderNum: null,
        preProductionQuantity: null,
        unplannedQuantity: null,
        cjCode: null,
        workshopName: null,
        lineId: null,
        lineName: null,
        planOnlineTime: null,
        planOfflineTime: null,
        dyZcNum: null,
        configuration: null,
        emiStand: null,
        displacement: null,
        ratedPower: null,
        machineType: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加部装预排产计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssemblyPreSchedulingPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改部装预排产计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssemblyPreSchedulingPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssemblyPreSchedulingPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除部装预排产计划编号为"' + ids + '"的数据项？').then(function () {
        return delAssemblyPreSchedulingPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sales/assemblyPreSchedulingPlan/export', {
        ...this.queryParams
      }, `assemblyPreSchedulingPlan_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "部装预排产计划导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('sales/assemblyPreSchedulingPlan/importTemplate', {}, `部装预排产计划模板.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    formalProduction(row){
      const loading = this.$loading({
        lock: true,
        text: '正在生成正式排产，请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const ids = row.id || this.ids;
      formalProduction(ids).then(response => {
        if(response.code==500){
          this.$modal.msgError(response.msg);
        }else{
          this.$modal.msgSuccess("排产成功！");
          this.getList();
        }
        loading.close();
      }).catch(reason=>{
        loading.close();
      });
    }
  }
};
</script>
